<template>
	<view>
		<view v-for="(item, i) in goodsList" class="goodsItem" :key="i" @click="navigateTo(item)">
			<view class="goodsImg">
				<image :src="item.goods_small_logo || defaultPic" />
			</view>
			<view class="goodsInfo">
				<view>{{item.goods_name}}</view>
				<view class="goodsPrice">¥ {{item.goods_price}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	
	import { $http } from '@escook/request-miniprogram';
	
	export default {
		data() {
			return {
				loading: false,
				defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',
				queryObj: {
					query: '',
					cid: '',
					pagenum: 0,
					pagesize: 10
				},
				goodsList: [],
				total: 0
			}
		},
		onPullDownRefresh: function() {
			this.queryObj.pagenum = 0;
			this.goodsList = [];
			this.getGoodsListData(() => {
				uni.stopPullDownRefresh();
			});
		},
		onLoad: function(options) {
			this.queryObj.query = options.query || '';
			this.queryObj.cid = options.cid || '';
			this.getGoodsListData();
		},
		onReachBottom: function() {
			this.getGoodsListData();
		},
		methods: {
				navigateTo: function(item) {
					uni.navigateTo({
						url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
					});
				},
				getGoodsListData: async function(cb) {
					if (this.loading) return;
					if (this.total && (this.queryObj.pagenum * this.queryObj.pagesize) >= this.total) {
						return;
					}
					this.loading = true;
					this.queryObj.pagenum++;
					const {data: res} = await $http.get('/goods/search', this.queryObj).catch(err => err);
					if (!res || res.meta.status !== 200) {
						return uni.$baseShowMsg();
					}
					this.goodsList = [...this.goodsList, ...res.message.goods];
					this.total = res.message.total;
					this.loading = false;
					cb && cb();
				}
		}
	}
</script>

<style lang="scss">
	.goodsItem {
		border-bottom: 1rpx solid #ccc;
		padding: 10px;
		display: flex;
	}
	.goodsImg {
		image {
			display: block;
			width: 190rpx;
			height: 190rpx;
		}
	}
	.goodsInfo {
		padding-left: 10rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		font-size: 28rpx;
	}
	.goodsPrice {
		color: #c00000;
		font-size: 36rpx;
	}
</style>
